;https://zhxk82@code.google.com/p/mcu-16f648a-uart-led-key-pwm-coos/
;tG7de6dN9Nw9
#include <p16f648a.inc>
;bank0 sysuse, bank1 app use, bank2 other or debug

EXTERN DPC;
EXTERN ISR;
EXTERN IDLE;
EXTERN SWITCH_TASK;check DPC, roltate switch task, 
EXTERN WAIT_SEMAPHORE;param1: signal id, param 2: b_reset_semaphore arfter recieved a signasl such like event
EXTERN SET_EVENT;param1: signal id
EXTERN RESET_EVENT;param 1 signal id.

CONSTANT MAX_TASKS=8;
CONSTANT MAX_SIGNALS=8;
CONSTANT SZ_TCB=3;pch,pcl,s_status
CONSTANT SS_DPC=0;soft status bit position
CONSTANT PZ_STATUS=2;position of status in TCB
CONSTANT SZ_TCBARRARY=SZ_TCB*MAX_TASKS
CONSTANT TS_SEMAPHORE0=0;task status semaphore0-7

UDATA 0x20;bank 0
    ipch            res 1;ISR variable
    status_temp     res 1;
    fsr_temp        res 1;

    c_temp          res 1;ALU variable
    w_temp          res 1
    s_temp          res 1;
    d_temp          res 1;
    b_temp          res 1

    last_ready_tcb  res 1;TCB variable
    last_wait_tcb   res 1;
    current_tcb     res 1;
    tcb_array       res SZ_TCBARRARY;

    last_signal     res 1;semaphore array
    signal_array    res MAX_SIGNALS;

UDATA_SHR 0x70;shared data section
    w_temp          res 1;interrup_w for ISR
    g_status        res 1;global status
    pcl_call        res 1;
    pch_call        res 1;funcation call return addr
    param1          res 1;
    param2          res 1;

YIELD MACRO local _exit;store pch to param2, pcl to param1,every task may have its own equivalent yield macro to store status reg etc.
    movf high _exit,w
    movwf pch_call;pch
    movf low _exit,w
    movwf pcl_call;pcl
    pagesel SWITCH_TASK
    goto SWITCH_TASK
    _exit:
ENDM

MEMCPY MACRO local _LOOP
    _LOOP:
    movwf s_temp,w
    movf fsr
    movwf indf,w
    movf w_temp
    movwf d_temp,w
    movf fsr
    movwf w_temp,w
    movf indf
    incf s_temp
    incf d_temp
    decf c_temp
    btfsc status,z
    goto _LOOP;?
ENDM

RET_CALL MACRO
    movwf pch_call,w
    movf pclath
    movwf pcl_call,w
    movf pcl
ENDM